package com.swpu.utils;

import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.models.*;
import com.swpu.common.CustomException;

public class SMSUtils {

	/**
	 * 使用AK&SK初始化账号Client
	 * @param accessKeyId
	 * @param accessKeySecret
	 * @return Client
	 * @throws Exception
	 */
	public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) {
		Config config = new Config()
				// 您的AccessKey ID
				.setAccessKeyId(accessKeyId)
				// 您的AccessKey Secret
				.setAccessKeySecret(accessKeySecret);
		// 访问的域名
		config.endpoint = "dysmsapi.aliyuncs.com";
		try {
			return new com.aliyun.dysmsapi20170525.Client(config);
		} catch (Exception e) {
			throw new CustomException("短信服务创建Client失败");
		}
	}

	public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param) {
		com.aliyun.dysmsapi20170525.Client client = SMSUtils.createClient("LTAI5tNc9ni3AFRk91msLBvb", "zoPjIUIwQJDL4qDYW5TmLfmudM1WnY");
		SendSmsRequest sendSmsRequest = new SendSmsRequest()
				.setSignName(signName)
				.setTemplateCode(templateCode)
				.setPhoneNumbers(phoneNumbers)
				.setTemplateParam("{\"code\":\""+param+"\"}");
		RuntimeOptions runtime = new RuntimeOptions();
		// 复制代码运行请自行打印 API 的返回值
		try {
			client.sendSmsWithOptions(sendSmsRequest, runtime);
		} catch (Exception e) {
			throw new CustomException("Client执行发送任务失败");
		}
	}
}
